package client;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;
import java.util.ArrayList;

public class ClientUI {
    final private ArrayList<MesHistory> mesHistories=new ArrayList<>();
    private Socket socket;
    public SendUtil su;
    public static void main(String[] args) {
        ClientUI cientUI=new ClientUI();
        cientUI.init();
    }
    //连接服务器的方法
    public Socket connection() {
        try {
            //尝试连接服务器
            Socket socket=new Socket("127.0.0.1", 100);
            return socket;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //界面设置
    public void init() {
        JFrame jframe=new JFrame("多人聊天室");
        jframe.setSize(700, 500);
        jframe.setDefaultCloseOperation(3);
        jframe.setLayout(null);
        //设置界面居中
        jframe.setLocationRelativeTo(null);
        //设置最大化按钮不可用
        jframe.setResizable(false);
        //历史消息框、并且添加滚动条
        JTextArea historyMsg=new JTextArea();
        JTextArea myHistoryMsg=new JTextArea();
        JScrollPane jsp=new JScrollPane(historyMsg);
        JScrollPane msp=new JScrollPane(myHistoryMsg);
        jsp.setBounds(5, 25, 400, 300);
        msp.setBounds(405,25,285,300);
        historyMsg.setEditable(false);
        myHistoryMsg.setEditable(false);
        jframe.add(jsp);
        jframe.add(msp);
        //发送消息框
        JTextArea sendMsg=new JTextArea();
        JScrollPane jsp2=new JScrollPane(sendMsg);
        jsp2.setBounds(5, 325, 550, 140);
        jframe.add(jsp2);
        //按钮
        JButton sendBut=new JButton("发送");
        JButton showHistoryMessage=new JButton("展示历史消息");
        JButton clearBut=new JButton("清空");
        JButton connectBut=new JButton("连接服务器");
        sendBut.setBounds(555, 364, 134, 60);
        showHistoryMessage.setBounds(555,425,134,40);
        clearBut.setBounds(555, 325, 134, 40);
        connectBut.setBounds(5, 1, 150, 22);
        jframe.add(connectBut);
        jframe.add(sendBut);
        jframe.add(clearBut);
        jframe.add(showHistoryMessage);
        //给按钮添加监听器
        ActionListener al=new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String command=e.getActionCommand();
                switch (command) {
                    case "发送":
                        if(socket!=null) {
                            String msg=sendMsg.getText().trim();
                            sendMsg.setText("");
                            historyMsg.append("我说："+msg+"\r\n");
                            //调用方法发送出去
                            su.send(msg);

                            MesHistory mesHistory=new MesHistory(msg+"\r\n");
                            mesHistories.add(mesHistory);
//                            System.out.println(mesHistories.get(0).getDate()+"|"+mesHistories.get(0).getMessage());
                        }
                        break;
                    case "清空":
                        sendMsg.setText("");
                        break;
                    case "连接服务器":
                        socket=ClientUI.this.connection();
                        if(socket==null) {
                            JOptionPane.showMessageDialog(null, "连接失败，请检查网络或重新连接！");
                        }else {
                            connectBut.setText("已连接服务器");
                            JOptionPane.showMessageDialog(null, "连接成功！");
                            //启动线程并且给服务器发送消息
                            ReceiveThread rt=new ReceiveThread(socket, historyMsg);
                            rt.start();
                            su=new SendUtil(socket);
                        }
                        break;
                    case "展示历史消息":
                        if (mesHistories.isEmpty()){
                            myHistoryMsg.setText("");
                            myHistoryMsg.append("-------你暂时没有历史记录---------\n");
                        }
                        else{
                            myHistoryMsg.setText("");
                            myHistoryMsg.append("--------我的历史消息记录---------\n");
                            for (MesHistory mes :
                                    mesHistories) {
                                myHistoryMsg.append(mes.getDate()+" ");
                                myHistoryMsg.append(mes.getMessage());
                            }
                            myHistoryMsg.append("----------------------------------------\n");
                        }
                    default:
                        break;
                }

            }
        };
        connectBut.addActionListener(al);
        sendBut.addActionListener(al);
        clearBut.addActionListener(al);
        showHistoryMessage.addActionListener(al);
        jframe.setVisible(true);
    }
}
